// pages/mine/selectAcademy/selectAcademy.js
const app = getApp()
var util = require("../../../utils/util.js")
const db = wx.cloud.database()
const _ = db.command
const collegeDB=db.collection('collegeData')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userOpenid: '',
    academyArray:[],//学院数组
    academy_opacity:[],
    academy_selected:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     let that = this
      that.setacademyArray("长江大学")
      that.setData({
        userOpenid:app.globalData.userOpenid
      })
      console.log(that.data.academyArray,that.data.academy_opacity)
  },

  //得到所有学院
  setacademyArray:function(college){
    let that = this
    collegeDB.where({
      college:college
    }).get({
      success:function(res){
       
        that.setData({
          academyArray:res.data[0].allAcademy,
        })
        console.log(college,"的所有专业",that.data.academyArray)
        let academy_length = that.data.academyArray.length
        for(var i=0;i<academy_length;i++){
          that.data.academy_opacity.push(0.5)
        }
        console.log(that.data.academy_opacity)
      }
    })
  },
  
   
    //点击每一个学院标签
    lighttheAcademy:function(e){
      console.log(e.target.id)
      let itemID = Number(e.target.id)
      let that=this
      let academy_opacity_item =  that.data.academy_opacity[itemID]
      if(0.5 == academy_opacity_item){
        that.data.academy_opacity[itemID] = 1
        that.data.academy_selected.push(itemID)
      }else{
        that.data.academy_opacity[itemID] = 0.5
        that.data.academy_selected.splice(that.data.academy_selected.findIndex(e => e === itemID), 1)
      }
      that.setData({
        academy_opacity:that.data.academy_opacity
      })
      console.log(that.data.academy_opacity,that.data.academy_selected)
    },


  //点击确定按钮
  clickBtnSure:function(){
    let that = this
    let select = that.data.academy_selected
    console.log("选择的标签",select)
      if(1==select.length){
        let openid = that.data.userOpenid
        console.log(that.data.userOpenid,"****",select[0])
        wx.cloud.callFunction({
          name: 'updataUserLabel',
          data: {
            openid: openid,
            userlabel:Number(select[0])
          },
          success: res => {
            console.log("更新成功")
            console.log("选择的标签",select[0])
            app.globalData.academy=Number(select[0])
            console.log(app.globalData)
            var pages = getCurrentPages()
            var beforePage = pages[pages.length-2] 
            // beforePage.setData({
            //   academy:util.getAcademybyNum(Number(select[0])),
            //   btn_opacity:1
            // })
            beforePage.setAcademyBtnTo(select[0])
             wx.navigateBack({
             })
          },
          fail: err => {
            // handle error
            console.log("更新失败")
          },
        })
      }else{
        wx.showToast({
          icon:"none",
          title: '最多选择一个标签哦~'
        })
      }
  }
})